<?php

declare(strict_types=1);

namespace app\common\lib;

class Show
{
    public static function json_show(int $code = 0,string $msg = "",array $data = [],int $http_code =200){
        $result = [
            "code"=>$code,
            "msg"=>$msg,
            "data"=>$data
        ];
        return json($result,$http_code);
    }
    public static function json_encode(array $data = []){
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    public static function getDistances($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {
        $EARTH_RADIUS = 6370.996; //地球半径系数
        $PI = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit == 2) {
            $distance /= 1000;
           $gongli= round($distance, $decimal).'km';
        }else{
            $gongli=round($distance, $decimal).'m';
        }

        return $gongli;
    }
    /**
     * 计算配送相隔距离
     */
    public static function getShippingDistances($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {
        $EARTH_RADIUS = 6370.996; //地球半径系数
        $PI = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit == 2) {
            $distance /= 1000;
            $gongli= round($distance, $decimal);
        }else{
            $gongli=round($distance, $decimal);
        }

        return $gongli;
    }

}